package com.demo.user.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.demo.commons.results.Results;
import com.demo.user.api.OauthService;
import com.demo.user.context.UserContextHolder;
import com.demo.user.domain.OauthToken;

/**
* @author yangjian
* @version 创建时间：2022年10月18日 下午4:23:30
* 
*/
@RestController
public class UserController {
	
	@Autowired
	private OauthService oauthService;
	
	@PostMapping("login")
	public Results login(String username) {
		return Results.success(createToken(username));
	}
	
	@GetMapping("getUser")
	public Object getUser() {
		return UserContextHolder.user();
	}
	
	private OauthToken createToken(String username) {
		Map<String, String> parameters = new HashMap<>();
		parameters.put("client_id", "test");
		parameters.put("client_secret", "123456");
		parameters.put("scope", "all");
		parameters.put("grant_type", "password");
		parameters.put("username", username);
		parameters.put("password", username);
		return oauthService.createAccessToken(parameters);
	}

}
